// 新增页面子表头部操作按钮组件

import React, {Component} from 'react';

import './index.less';

import {toast, base} from 'nc-lightapp-front';

import language from '../../../../language';

const {
    NCButton
} = base;

class TableOperator extends Component {
    constructor(props) {
        super(props);

        this.state = {
            buttonDisable: false
        };

        this.clickHandle = this.clickHandle.bind(this);
    }

    clickHandle(btnCode) {
        return () => {
            const {
                cardTable: {
                    getCheckedRows
                },
                moduleId,
                deleteRow,
                copyRow,
                addRow
            } = this.props;

            const selected = getCheckedRows(moduleId);

            if (btnCode === 'AddLine') {
                addRow(moduleId, undefined, {}, false);
            } else if (btnCode === 'DelLine') {
                let selectedIndex = selected.map((item) => {
                    return item.index;
                });
                deleteRow(moduleId, selectedIndex);
            } else if (btnCode === 'CopyLine') {
                if (selected.length <= 0) {
                    toast({
                        color: 'warning',
                        content: language['hi6007-000170']/* 国际化处理： 请选择复制的行*/
                    });
                } else {
                    let selectedData = [];
                    selected.map((item) => {
                        selectedData.push(item.data);
                    });
                    copyRow(moduleId, selectedData);
                }
            }
        }
    }

    componentDidMount() {
        const {
            button: {
                setButtonDisabled
            },
            addPageTemplate,
            moduleId
        } = this.props;

        if (addPageTemplate.busiSet.includes(moduleId)) {
            this.setState({
                buttonDisable: true
            });
        }
    }

    render() {
        const {
            button: {
                createButtonApp,
                getButtons
            },
            subTableEditable
        } = this.props;

        const {
            buttonDisable
        } = this.state;

        let btnConf = getButtons();
        let delBtn, addBtn;
        btnConf.forEach(btn => {
            if (btn.key === 'DelLine') {
                delBtn = btn;
            } else if (btn.key === 'AddLine') {
                addBtn = btn;
            }
        })

        return (
            <div class="add-new-page-table-operator">
                {
                    subTableEditable &&
                    <div>
                        {addBtn ? <NCButton
                            colors="normal"
                            onClick={this.clickHandle('AddLine')}
                            disabled={buttonDisable}
                        >
                            {addBtn.title}
                        </NCButton> : null}
                        {delBtn ? <NCButton
                            colors="normal"
                            onClick={this.clickHandle('DelLine')}
                            disabled={buttonDisable}
                        >
                            {delBtn.title}
                        </NCButton> : null}

                    </div>
                }
            </div>
        );
    }
}

/**
 * 产品突然又说不要复制功能了，为了以后他们反悔所以还是保存下吧
 * <NCButton
 colors="normal"
 onClick={this.clickHandle('CopyLine')}
 disabled={buttonDisable}
 >
 {language['hi6007-000054']}
 </NCButton>
 *
 *  */
export default TableOperator;
